package com.zjitc.config;

import com.zjitc.mapper.BookMapper;
import com.zjitc.mapper.UserMapper;
import org.mybatis.spring.SqlSessionFactoryBean;
import org.mybatis.spring.mapper.MapperFactoryBean;
import org.springframework.context.annotation.Bean;

import javax.sql.DataSource;

public class MybatisConf {

    //SqlSessionFactoryBean配置
    @Bean
    public SqlSessionFactoryBean sqlSessionFactoryBean(DataSource dataSource) {
        SqlSessionFactoryBean sessionFactoryBean = new SqlSessionFactoryBean();

        sessionFactoryBean.setDataSource(dataSource);

        sessionFactoryBean.setTypeAliasesPackage("com.zjitc.pojo");

        return sessionFactoryBean;
    }

    @Bean
    MapperFactoryBean<UserMapper> userMapperFactoryBean(SqlSessionFactoryBean sqlSessionFactoryBean) throws Exception {
        MapperFactoryBean<UserMapper> mapperFactoryBean = new MapperFactoryBean<>();

        mapperFactoryBean.setSqlSessionFactory(sqlSessionFactoryBean.getObject());
        mapperFactoryBean.setMapperInterface(UserMapper.class);

        return mapperFactoryBean;
    }

    @Bean
    MapperFactoryBean<BookMapper> bookMapperFactoryBean(SqlSessionFactoryBean sqlSessionFactoryBean) throws Exception {
        MapperFactoryBean<BookMapper> mapperFactoryBean = new MapperFactoryBean<>();

        mapperFactoryBean.setSqlSessionFactory(sqlSessionFactoryBean.getObject());
        mapperFactoryBean.setMapperInterface(BookMapper.class);

        return mapperFactoryBean;
    }


}